import moment from 'moment';
import { Tag, Space } from 'antd';
import { FIELD_TYPES } from '@/constants/enums';
import { renderOperations } from './operation';
const renderColumn = (column) => {
  switch (column.type) {
    case FIELD_TYPES.datetime:
      column.render = (value) => moment(value).format('YYYY-MM-DD HH:mm:ss')
      break;
    case FIELD_TYPES.switch:
      column.render = (value) => {
        const { title } = column.data.find(item => item.value === value);
        return <Tag color={value ? 'green' : 'red'}> {title} </Tag>
      }
      break;
    default:
      break;
  }
}

export function renderColumns(data, onAction) {
  if (data) {
    const { fields, record } = data;
    return fields
      .filter(field => !field.hideInColumn)
      .map(field => {
        const column = { ...field };
        column.key = column.dataIndex = field.name;
        renderColumn(column);
        return column;
      }).concat({
        title: '操作', dataIndex: 'operations', key: 'operations', render: (_, row) => (
          <Space>
            {renderOperations(record, onAction, row)}
          </Space>
        )
      });
  }
  return [];
}